using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
public class SimpleComputeUI : MonoBehaviour
{
    public RectTransform PeripheralGroupPos;
    
    public RectTransform TerminaGroupPos;
    
    public RectTransform LinkStrategyGroupPos;
    
    public Text CurPeripheralText;

    public Text CurTerminaText;
    
    public Text CurLinkStrategyText;
    
    public GameObject ButtonPrefab;

    public Text ResultText;
    
    public Button SendBtn;
    
    private ComputerMediator mediator;
    
    private List<Button> btnList = new List<Button>();

    private int curPeripheralIndex;
    
    private int curTerminaIndex;
    
    private int curLinkStrategyIndex;
    
    void Awake()
    {
        mediator = ComputerMediator.GetInstance();
        InitButtons();
    }
    
    private void InitButtons()
    {
        var peripheralslist = mediator.GetPeripherals();
        for (int i = 0; i < peripheralslist.Count; i++)
        {
            int peripheralIndex = i;
            CreateItem(peripheralslist[i].GetElementName(), peripheralIndex, eItemType.Peripheral);
        }
        
        var terminalist = mediator.GetTerminas();
        for (int i = 0; i < terminalist.Count; i++)
        {
            int  terminaIndex = i;
            CreateItem(terminalist[i].GetElementName(), terminaIndex, eItemType.Termina);
        }
        
        var LinkStrategylist = mediator.GetLinkStrategys();
        for (int i = 0; i < LinkStrategylist.Count; i++)
        {
            int LinkStrategyIndex = i;
            CreateItem(LinkStrategylist[i].GetElementName(), LinkStrategyIndex, eItemType.LinkStrategy);
        }
        
        SendBtn.onClick.AddListener(OnClickSendBtn);
    }
    
    private void OnClickSendBtn()
    {
        mediator.ConnectPeripheralAndTermina(curPeripheralIndex, curLinkStrategyIndex, curTerminaIndex );
        ResultText.text = mediator.SendMessage();
    }

    private void CreateItem(string name, int index, eItemType itemType)
    {
        RectTransform pos = null;
        switch (itemType)
        {
            case eItemType.Peripheral:
                pos = PeripheralGroupPos;
                break;
            case eItemType.Termina:
                pos = TerminaGroupPos;
                break;
            case eItemType.LinkStrategy:
                pos = LinkStrategyGroupPos;
                break;
            default:
                break;
        }

        if (pos != null)
        {
            GameObject newItem = Instantiate(ButtonPrefab.gameObject, pos);
            if (newItem != null)
            {
                Button btn = newItem.GetComponent<Button>();
                if (btn != null)
                {
                    btn.onClick.AddListener(()=>OnClickTypeBtn(name, index, itemType));
                    var text = newItem.transform.Find("Text").GetComponent<Text>();
                    if (text != null)
                    {
                        text.text = name;
                    }
                    btnList.Add(btn);
                }
            }
        }

    }

    private void OnClickTypeBtn(string name, int index, eItemType itemType)
    {
        switch (itemType)
        {
            case eItemType.Peripheral:
                curPeripheralIndex = index;
                CurPeripheralText.text = name;
                break;
            case eItemType.Termina:
                curTerminaIndex = index;
                CurTerminaText.text = name;
                break;
            case eItemType.LinkStrategy:
                curLinkStrategyIndex = index;
                CurLinkStrategyText.text = name;
                break;
            default:
                break;
        }
    }

    private void OnDestroy()
    {
        mediator.ReSetAll();
        
        for (int i = 0; i < btnList.Count; i++)
        {
            btnList[i].onClick.RemoveAllListeners();
        }
        
        btnList.Clear();
        btnList = null;

        SendBtn.onClick.RemoveAllListeners();
    }
}
